<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:cv="feurerworks.com/ns/hr/cv"
  xmlns:ad="livcos.org/ns/admin"
  xmlns="http://www.w3.org/1999/XSL/Format"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:v="livcos.org/ns/view"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="doc cv ad v f xs fo">

	<xsl:import href="../../../../../base.xsl"/>
	<xsl:import href="../../../../livcos.org/ns/doc/outline.xsl"/>
	
	<xsl:attribute-set name="block2">
		<xsl:attribute name="margin-left">-6mm</xsl:attribute>
		<xsl:attribute name="margin-right">-4mm</xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:attribute-set name="cell">
		<xsl:attribute name="padding">2pt</xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:attribute-set name="label">
		<xsl:attribute name="text-align">right</xsl:attribute>
		<xsl:attribute name="font-style">italic</xsl:attribute>
		<xsl:attribute name="font-size">7pt</xsl:attribute>
		<xsl:attribute name="line-height">10pt</xsl:attribute>
		<xsl:attribute name="color">#333333</xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:attribute-set name="content">
	</xsl:attribute-set>
	
	<xsl:template name="row">
		<xsl:param name="label"/>
		<xsl:param name="content"/>
		<xsl:param name="label2"/>
		<xsl:param name="content2"/>
		<table-row keep-with-previous.within-page="always">
			<table-cell xsl:use-attribute-sets="label cell">
				<block xsl:use-attribute-sets="block2">
					<xsl:call-template name="apply-or-print">
						<xsl:with-param name="item" select="$label"/>
					</xsl:call-template>
				</block>
			</table-cell>
			<table-cell xsl:use-attribute-sets="content cell">
				<block xsl:use-attribute-sets="block2">
					<xsl:call-template name="apply-or-print">
						<xsl:with-param name="item" select="$content"/>
					</xsl:call-template>
				</block>
			</table-cell>
			<xsl:if test="$label2">
				<table-cell xsl:use-attribute-sets="label cell">
					<block xsl:use-attribute-sets="block2">
						<xsl:call-template name="apply-or-print">
							<xsl:with-param name="item" select="$label2"/>
						</xsl:call-template>
					</block>
				</table-cell>
				<table-cell xsl:use-attribute-sets="content cell">
					<block xsl:use-attribute-sets="block2">
						<xsl:call-template name="apply-or-print">
							<xsl:with-param name="item" select="$content2"/>
						</xsl:call-template>
					</block>
				</table-cell>
			</xsl:if>
		</table-row>
	</xsl:template>
	
	<xsl:template name="apply-or-print">
		<xsl:param name="item"/>
		<xsl:param name="mode"/>
		<xsl:choose>
			<xsl:when test="$item instance of node()*">
				<xsl:apply-templates select="$item"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$item"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="cv:* | text | @title">
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="fo:*">
		<xsl:sequence select="."/>
	</xsl:template>
	
	<xsl:template match="cv:particulars">
		<xsl:variable name="p" select="f:entity(@ref)"/>
		<block xsl:use-attribute-sets="block">
			<table table-layout="fixed" width="100%">
				<table-column column-width="14%"/>
				<table-column column-width="29%"/>
				<table-column column-width="14%"/>
				<table-column column-width="29%"/>
				<table-body>
					<xsl:call-template name="row">
						<xsl:with-param name="label" select="cv:name"/>
						<xsl:with-param name="content" select="concat($p/ad:first-name, ' ', $p/ad:name)"/>
						<xsl:with-param name="label2" select="cv:birthday"/>
						<xsl:with-param name="content2" 
							select="format-date(xs:date($p/ad:birthday), '[D] [MNn] [Y]', /*/@language, (), /*/@country)"/>
					</xsl:call-template>
					<xsl:call-template name="row">
						<xsl:with-param name="label" select="cv:address"/>
						<xsl:with-param name="content">
							<xsl:apply-templates select="$p/ad:address[@type = 'main']" mode="cv"/>
						</xsl:with-param>
						<xsl:with-param name="label2" select="cv:native"/>
						<xsl:with-param name="content2">
							<xsl:apply-templates select="$p/ad:address[@type = 'native']" mode="cv"/>
						</xsl:with-param>
					</xsl:call-template>
					<xsl:call-template name="row">
						<xsl:with-param name="label" select="cv:phone"/>
						<xsl:with-param name="content">
							<xsl:apply-templates select="$p/ad:phone[@type = 'business']" mode="cv"/>
						</xsl:with-param>
						<xsl:with-param name="label2" select="cv:label(cv:citizen)"/>
						<xsl:with-param name="content2" select="cv:val(cv:citizen, $p/ad:citizen)"/>
					</xsl:call-template>
					<xsl:call-template name="row">
						<xsl:with-param name="label" select="cv:email"/>
						<xsl:with-param name="content">
							<xsl:apply-templates select="$p/ad:email[@type = 'business']" mode="cv"/>
						</xsl:with-param>
						<xsl:with-param name="label2" select="cv:label(cv:sex)"/>
						<xsl:with-param name="content2" select="cv:val(cv:sex, $p/ad:sex)"/>
					</xsl:call-template>
					<xsl:call-template name="row">
						<xsl:with-param name="label" select="/.."/>
						<xsl:with-param name="content" select="/.."/>
						<xsl:with-param name="label2" select="cv:label(cv:civil-state)"/>
						<xsl:with-param name="content2" select="cv:val(cv:civil-state, $p/ad:civil-state)"/>
					</xsl:call-template>
				</table-body>
			</table>
		</block>
	</xsl:template>
  
	<xsl:template match="ad:address[@ref]" mode="#all">
		<xsl:apply-templates select="f:entity(@ref)" mode="#current"/>
	</xsl:template>
	
	<xsl:template match="ad:address" mode="cv">
		<xsl:if test="ad:street">
			<block keep-with-previous.within-page="always" keep-with-next.within-page="always">
				<xsl:value-of select="ad:street" />&#160;<xsl:value-of select="ad:house" />
			</block>
		</xsl:if>
		<block>
			<xsl:value-of select="upper-case(ad:country)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="ad:zipCode | ad:zip"/>&#160;<xsl:value-of select="ad:city"/>
		</block>
	</xsl:template>
	
	<xsl:template match="ad:phone" mode="cv">
		<xsl:text>+</xsl:text>
		<xsl:value-of select="@country"/>
		<xsl:text> (0</xsl:text>
		<xsl:value-of select="@area"/>
		<xsl:text>) </xsl:text>
		<xsl:value-of select="substring(@number,1,3)"/>
		<xsl:text>&#160;</xsl:text>
		<xsl:value-of select="substring(@number,4)"/>
	</xsl:template>
	
	<xsl:template match="ad:email" mode="cv">
		<xsl:value-of select="@name"/>
		<xsl:text>@</xsl:text>
		<xsl:value-of select="@domain"/>
	</xsl:template>
	
	<xsl:template match="ad:address" mode="work">
		<xsl:value-of select="ad:city"/>
		<xsl:text>, </xsl:text>
		<xsl:value-of select="upper-case(ad:country)"/>
	</xsl:template>
	
	<xsl:template match="cv:activities | cv:skills | cv:interests">
		<block xsl:use-attribute-sets="block">
			<table table-layout="fixed" width="100%">
				<table-column column-width="14%"/>
				<table-column column-width="86%"/>
				<table-body>
					<xsl:apply-templates select="cv:*"/>
				</table-body>
			</table>
		</block>
	</xsl:template>
	
	<xsl:template match="cv:education">
		<xsl:call-template name="row">
			<xsl:with-param name="label" select="cv:periode"/>
			<xsl:with-param name="content" select="cv:description | cv:faculty"/>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template match="cv:periode">
		<!-- xs:date() cannot handle incomplete strings (only year: '2001', only year-month: '2001-05') -->
		<!-- only year -->
		<xsl:variable name="s" select="if (contains(@start, '-')) then substring-before(@start, '-') else @start"/>
		<xsl:variable name="e" select="if (contains(@end, '-')) then substring-before(@end, '-') else @end"/>
		<xsl:value-of select="$s"/>
		<xsl:if test="not(@end) or ($e != $s)">
			<xsl:text> - </xsl:text>
			<xsl:value-of select="$e"/>
			<xsl:if test="not(@end)">...&#160;&#160;&#160;&#160;</xsl:if>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cv:faculty">
		<xsl:text>; </xsl:text>
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="cv:work">
		<xsl:call-template name="row">
			<xsl:with-param name="label" select="cv:periode"/>
			<xsl:with-param name="content">
				<block>
					<xsl:apply-templates select="cv:profession"/>
					<xsl:value-of select="../text[@key = 'at']"/>
					<xsl:apply-templates select="f:entity(@company)"/>
				</block>
				<list-block>
					<xsl:apply-templates select="cv:task"/>
				</list-block>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template match="cv:task">
		<list-item>
			<list-item-label start-indent="2mm">
				<block>-</block>
			</list-item-label>
			<list-item-body start-indent="4mm">
				<block><xsl:apply-templates select="node()"/></block>
			</list-item-body>
		</list-item>
	</xsl:template>
	
	<xsl:template match="cv:language | cv:category">
		<xsl:call-template name="row">
			<xsl:with-param name="label" select="cv:label"/>
			<xsl:with-param name="content" select="cv:* except cv:label"/>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template match="cv:product">
		<xsl:call-template name="row">
			<xsl:with-param name="label" select="@title"/>
			<xsl:with-param name="content" select="node()"/>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template match="cv:category/cv:interest">
		<xsl:if test="position() > 1">; </xsl:if>
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	
	<xsl:template match="cv:category/cv:product">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:value-of select="@title"/>
		<xsl:text> </xsl:text>
		<xsl:value-of select="@release"/>
	</xsl:template>
	
	<xsl:template match="cv:experiences">
		<xsl:apply-templates select="cv:*"/>
	</xsl:template>
	
	<xsl:template match="cv:project">
		<v:chapter id="{@id}">
			<v:title><xsl:apply-templates select="cv:title"/></v:title>
			<xsl:apply-templates select="doc:* | fo:*"/>
			<xsl:if test="cv:customer | cv:statistic">
				<block xsl:use-attribute-sets="block">
					<table table-layout="fixed" width="100%" keep-with-previous.within-page="always">
						<table-column column-width="13%"/>
						<table-column column-width="87%"/>
						<table-body>
							<xsl:if test="cv:customer">
								<xsl:call-template name="row">
									<xsl:with-param name="label" select="ancestor::*/text[@key = 'customer'][1]"/>
									<xsl:with-param name="content" select="cv:customer"/>
								</xsl:call-template>
							</xsl:if>
							<xsl:if test="cv:statistic">
								<xsl:call-template name="row">
									<xsl:with-param name="label" select="ancestor::*/text[@key = 'statistic'][1]"/>
									<xsl:with-param name="content" select="cv:statistic"/>
								</xsl:call-template>
							</xsl:if>
						</table-body>
					</table>
				</block>
			</xsl:if>
		</v:chapter>
	</xsl:template>
	
	<xsl:template match="cv:customer">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:value-of select="f:entity(@company)/ad:title"/>
	</xsl:template>
	
	<xsl:template match="cv:statistic">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="ad:company">
		<xsl:value-of select="ad:title"/>
		<xsl:text>, </xsl:text>
		<xsl:apply-templates select="ad:address[@type = 'main']" mode="work"/>
	</xsl:template>
	
	<xsl:function name="cv:label">
		<xsl:param name="spec"/>
		<xsl:sequence select="if ($spec/cv:label) then $spec/cv:label/node() else $spec/node()"/>
	</xsl:function>
	
	<xsl:function name="cv:val">
		<xsl:param name="spec"/>
		<xsl:param name="val"/>
		<xsl:variable name="_v" select="$spec/cv:val[@key = $val]"/>
		<xsl:sequence select="if ($_v) then $_v/node() else $val/node()"/>
	</xsl:function>
	
</xsl:stylesheet>
